﻿/**
 * @usage
 * 视图按钮UI
 * 		/**---------------------------------
 * 		*	限制性设定:
 * 		*	iLabel			包含文本框的MovieClip
 *  	*	iLabel_txt		文本框
 *  	*	iHitArea		点击区域
 *  	**--------------------------------/
 */
package org.nickro.project.widgets {
    import flash.display.MovieClip;
	import flash.events.MouseEvent;
    import flash.text.TextField;

	import org.nickro.project.sound.SoundChan;
    public class PushButton extends MovieClip {
        public var pID:String;
        public var iHitArea:MovieClip;
		public var iLabel_txt:TextField;
		
		public var pClickEventType:String = CLICK_PUSH_BUTTON;
		protected var pMouseOver:Boolean = false;
		protected var pTrackAsMenu:Boolean = false;
		protected var pUsingSounds:Boolean;
        protected var pUsingLabel:Boolean;
        protected var pClickChan:SoundChan;
        protected var pEnabled:Boolean;
        protected var pRollChan:SoundChan;

        public static const CLICK_PUSH_BUTTON:String = "clickPushButton";
        public static const FRAME_DOWN:int = 4;
        public static const FRAME_OVER:int = 3;
        public static const FRAME_UP:int = 2;
        public static const FRAME_DISABLED:int = 1;

        public function PushButton(){
            pID = name;
            enable(true);
            pUsingLabel = (this["iLabel_txt"] is TextField);
            if (!pUsingLabel && this["iLabel"] is MovieClip){
                iLabel_txt = this["iLabel"]["iLabel_txt"];
				trace('Debug : OK');
                pUsingLabel = (iLabel_txt is TextField);
            };
            hitArea = iHitArea;
            buttonMode = true;
            mouseEnabled = true;
            mouseChildren = false;
            addEventListener(MouseEvent.ROLL_OVER, rollOverListener);
            addEventListener(MouseEvent.ROLL_OUT, rollOutListener);
            addEventListener(MouseEvent.MOUSE_DOWN, mouseDownListener);
            addEventListener(MouseEvent.MOUSE_UP, mouseUpListener);
            addEventListener(MouseEvent.CLICK, clickListener);
        }
		
		/**--------------------------------
		 * 事件处理 Event Listeners
		 * 
		 ------------------------*/
        protected function rollOverListener(e:MouseEvent):void{
            if (pEnabled){
                pMouseOver = true;
                gotoAndStop(FRAME_OVER);
                if (((pUsingSounds) && (pRollChan))){
                    pRollChan.play();
                };
            };
        }
		
        protected function clickListener(e:MouseEvent):void{
            if (pEnabled){
                if (!pTrackAsMenu){
                    clickHandler();
                };
            };
        }
		protected function mouseUpListener(e:MouseEvent):void{
            if (pEnabled){
                gotoAndStop((pMouseOver) ? FRAME_OVER : FRAME_UP);
                if (pTrackAsMenu){
                    clickHandler();
                };
            };
        }
        
        protected function mouseDownListener(e:MouseEvent):void{
            if (pEnabled){
                gotoAndStop(FRAME_DOWN);
            };
        }
		
        protected function rollOutListener(e:MouseEvent):void{
            if (pEnabled){
                pMouseOver = false;
                gotoAndStop(FRAME_UP);
            };
        }
		/**-----------------------------
		 * Public Methods
		 ----------------------------*/
		 /**
		  * 
		  * @param	btnLabel		按钮文本值
		  * @param	btnID			按钮ID
		  * @param	eventListener	点击处理函数
		  * @param	eventType		事件类型
		  */
		 public function initialize(btnLabel:String, btnID:String = "", eventListener:Function=null, eventType:String="clickPushButton"):void{
            if ((btnLabel is String)){
                setLabel(btnLabel);
            };
            if (btnID != "") pID = btnID;
            pClickEventType = eventType;
            if (eventListener != null){
                addEventListener(eventType, eventListener);
            };
        }
		
		public function clickHandler():void{
            if (pEnabled){
                dispatchEvent(new Event(pClickEventType, true));
                if (((pUsingSounds) && (pClickChan))){
                    pClickChan.play();
                };
            };
        }
		
		public function enable(onFlag:Boolean, showFlag:Boolean=true):void{
            pEnabled = onFlag;
            useHandCursor = pEnabled;
            if (showFlag){
                gotoAndStop((pEnabled) ? FRAME_UP : FRAME_DISABLED);
            };
        }
		
		public function setSoundChans(rollChan:SoundChan, clickChan:SoundChan):void{
            pRollChan = rollChan;
            pClickChan = clickChan;
            pUsingSounds = (((pRollChan is SoundChan)) || ((pClickChan is SoundChan)));
        }
		
		public function setAutoSizeDirection(auto_size:String="none"):void{
            iLabel_txt.autoSize = auto_size;
        }
		
        public function setTrackAsMenu(onFlag:Boolean):void{
            pTrackAsMenu = onFlag;
        }
		
        public function setLabel(labelHtml:String, auto_size:String="none"):void{
			trace("Debug :" + iLabel_txt);
            if(iLabel_txt){
				iLabel_txt.htmlText = labelHtml;
            	setAutoSizeDirection(auto_size);
			}
        }
		public function getLabel(asHTML:Boolean=false):String{
            return ((asHTML) ? iLabel_txt.htmlText : iLabel_txt.text);
        }
		
        public function isEnabled():Boolean{
            return (pEnabled);
        }
    }
}
